﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TsamIRC
{
    class Util
    {
        /// <summary>
        /// Parse and validates the cmdl arguments
        /// 
        /// </summary>
        /// <param name="args">string args[] from cmdl</param>
        /// <returns></returns>
        public Params parseCmdl(string[] args)
        {
            Params rslt = new Params();
            if (args.Length < 1 || args.Length > 3)
            {
                rslt.valid = false;
            }
            else
            {
                rslt.valid = false;
                UriHostNameType t = Uri.CheckHostName(args[0]);
                if (t.ToString() == ("Dns") || t.ToString() == ("IPv4"))
                {
                    rslt.host = args[0];
                    rslt.valid = true;
                }
                if (args.Length >= 2)
                    rslt.nick = args[1]; //validate?
                else
                    rslt.nick = "";
                if (args.Length == 3)
                {
                    int p;
                    if (int.TryParse(args[2], out p))
                        rslt.port = p;
                    else
                        rslt.port = 6667;
                }
                else
                    rslt.port = 6667;
            }
            echoParams(rslt);
            return rslt;
        }
        /// <summary>
        /// Echos out the parameters supplied by the user
        /// </summary>
        /// <param name="p"></param>
        public void echoParams(Params p)
        {
            if (p.valid)
            {
                string prompt= "Connecting to " + p.host + "@" + p.port;
                if (p.nick != "")
                    prompt += " as " + p.nick;
                //Console.WriteLine(prompt);                                        //ATH
            }
            else
            {
                Console.WriteLine("-- Incorrect parameters.");
                Console.WriteLine("-- IRCClient <server> [Nick] [port number]");
            }
        }

        /// <summary>
        /// Parses the messages recieved from server to a struct
        /// </summary>
        /// <param name="_resp"></param>
        /// <returns></returns>
        public IrcMsg parseResponse(string _resp)
        {
            string part1, part2, part3;
            IrcMsg rslt = new IrcMsg();
            //check for null exception
            if (_resp != null)
            {
                part1 = _resp;
                if (part1[0] == ':')
                {
                    int pos = part1.IndexOf(' ');
                    rslt.prefix = part1.Substring(1, pos - 1);
                    part2 = part1.Substring(pos);
                }
                else
                {
                    part2 = " " + part1;
                }
                //cut of trailer which is preceded with " :"
                int tPos = part2.LastIndexOf(" :");
                if (tPos > 0)
                {
                    rslt.trailer = part2.Substring(tPos + 2);
                    part3 = part2.Substring(1, tPos);
                }
                else
                {
                    rslt.trailer = part2;
                    part3 = part2;
                }

                string[] pars = System.Text.RegularExpressions.Regex.Split(part3, " ");
                rslt.command = pars[0];
                if (pars.Length > 0)
                {
                    string[] tmp = new string[pars.Length - 1];
                    for (int i = 1; i < pars.Length - 1; i++)
                    {
                        if (pars[i] != "" && pars[i] != null)
                            tmp[i - 1] = pars[i];
                    }
                    rslt.parameters = tmp;
                }
                //and the first is the command
                rslt.command = pars[0];
            }
            else {
                rslt.command = "ERR_PARSING";
                rslt.trailer = "NULL string recieved";

            }
            return rslt;
        }
    }
}
